
package Sgdb::Helper ;

use Sgdb::Utils qw(:DEFAULT
                   bind_search_mechanism
                   localconfig
                 ) ;

# use 5.10.0 ;                    # support new features
use warnings ;
use strict ;
use integer ;
use bytes ;

sub keyboard_shortcut {
    sub txt_gen { <<EndDOc }
:general
C-plus:	 (C-+) Increase Font Size
C-minus: (C--) Decrease Font Size
C-question:	 (C-?) keyboard shortcut help popup
Escape:  Close help popup

EndDOc

    sub txt_sgdb { $INC{'Sgdb/ServicePane.pm'} ? <<EndDoc : '' }
:nevigate
C-o:    Open Symbol File
C-l:	Open new target
C-i:	Open Swuch Pane
C-I:	Open Swuch Window
C-u:	Focus on Command Prompt
C-s/r:  Search forward/backward
C-S/R:  Search forward/backward (case sensitive)

:source pane
C-Tab:	 Next source pane
C-S-Tab: Previous source pane

:gdb command
C-greater:	 (C->) tfind   (next trace frame)
C-less:	 (C-<) tfind - (prev trace frame)
C-bracketright:	 (C-]) associative source line - forward
C-bracketleft:	 (C-[) associative source line - backward
C-LeftMouse:  info line on Captured/Highlighted Address/String

EndDoc

    sub txt_swuch { $INC{'Swuch/Window.pm'} ? <<EndDoc : '' }
:swuch
C-L:	 Focus on address entry
C-I:	  Focus on inline entry
C-H:	  toggle header
C-S-Left:	 switch director
C-S-Right:	 switch director
C-S-Up:	         switch director
C-S-Down:	 switch director
C-Down/Up:	 scroll inline pane
C-S-End:	 scroll down all inline panes
Tab:	 Inline Completion
<F7>:	    symmwin's F7 cli
<F2>:	    cd (change director shortcut)

EndDoc

    my @res ;

    for (split /^/, join '', &txt_gen, &txt_sgdb, &txt_swuch ) {
          if (/^\s*(\S+):\s*(.*)/) {
              my ($k, $v) = ($1, $2) ;
              $k =~ s/\bc\-/Control\-/i ;
              $k =~ s/\bs-/Shift\-/i ;
              push @res, "$k " . (' ' x (20 - length($k))), 'key', "$v\n", 'value'
          }
          elsif (/^:\s*(.*)/) {
              push @res, "* $1\n", 'title'
          }
      }
    @res
}

sub help_topics {
    ( 'help topics', 'key')
}

sub fill_keys {
    my ($self, $topf) = @_ ;

    my $t = $topf
      -> Frame ( pref 'helper-frame' )
        -> pack ( qw/-fill both -expand 1/ )
          -> ROText ( pref 'helper-text' ) ;

    pref_tags $t, 'helper', qw/key value title/ ;
    $t -> insert ( end => keyboard_shortcut() ) ;
    $t -> configure ( -height => $t->index('end') =~ /(\d+)/ ) ;
    $t -> pack ( qw/-fill both -expand 1 -pady 0 -padx 0 / ) ;

    $topf
}

sub new {
    my $self = shift or die 'whoeami' ;
    my $topf = shift or die 'top frame please' ;
    sub linkbind($$$) {
        my ($t, $tag, $sub) = @_ ;
        $t -> tagBind ( $tag, '<1>',     [$sub, $t, 'current']) ;
        $t -> tagBind ( $tag, '<Return>',[$sub, $t, 'insert' ]) ;
        $t -> tagBind ( $tag, '<Enter>', [$t, 'configure', -cursor, 'arrow']) ;
        $t -> tagBind ( $tag, '<Leave>', [$t, 'configure', -cursor, 'xterm']) ;
    }

    my $t = $topf               # add scroll !
      -> Scrolled (qw/ROText -scrollbars e -relief raised -borderwidth 1/, pref 'helper-text' )
       -> pack (qw/-side top -anchor nw -fill both -expand 1 -padx 0 -pady 0 /) ;

    pref_tags $t, helper => qw/filename glink title data bttop/ ;
    linkbind  $t, glink  => \&handle_clicked_glink ;
    linkbind  $t, bttop  => \&handle_back_to_top ;
    bind_search_mechanism $t ;
    $topf -> bind ('<Control-w>', [$topf->toplevel, 'destroy']) ;

    $t -> insert (end => "\n Answers: \n", 'title' ) ;
    $t -> markSet('tot', '1.0') ;
    $t -> markGravity('tot', 'right') ;

    my $faqd = localconfig 'faq-pod-directory' ;
    for my $f (glob "$faqd/*.pod") {
        open F, $f or return telthemloud "can't open $f: $!" ;
        my ($name) = $f =~ m{([^/]+)\.pod} ;
        $name =~ y/_/ / ;
        $t -> insert (tot => "\n  $name\n\n", 'filename') ;
        $t -> insert (end => "\n  $name\n\n", 'filename') ;
        local $/ ;
        my $F = <F> ;
        while ($F =~ /\n=head3\s*(.*?\n)(.*?)(?:(?=\n=)|\z)/sg ) {
            my ($q, $a) = ($1, $2) ;
            $t -> insert( tot => $q, 'glink' ) ;
            $t -> insert( end => $q, 'title', $a, 'data') ;
            $t -> insert( end => "Back To Top\n", ['data', 'bttop']) ;
        }
    }
    $t
}

sub handle_click(@) {
    my ($btt, $t, $type) = (shift, shift, shift) ;
    $type = shift while ref $type ;
    my $c = $t -> index ($type) ;
    my $q = ($btt ?
             $t -> get( $t -> tagPrevrange( title => $c )) :
             $t -> get("$c linestart", "$c lineend") ) ;
    my $loc = $t -> search ($q, $btt ? '1.0' : 'tot') ;
    $t -> yview( $loc ) ;
    $t -> markSet(insert => $loc ) ;
}

sub handle_clicked_glink { handle_click 0, @_ }
sub handle_back_to_top   { handle_click 1, @_ }
'end'
